from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:
    return """
        CREATE TABLE IF NOT EXISTS `sport_types` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `created_at` DATETIME(6) NOT NULL  DEFAULT CURRENT_TIMESTAMP(6),
    `updated_at` DATETIME(6) NOT NULL  DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `code` VARCHAR(50) NOT NULL UNIQUE COMMENT '内部代码（例：outdoor_run）',
    `name_cn` VARCHAR(50) NOT NULL  COMMENT '中文名称（例：户外跑）',
    `name_en` VARCHAR(50) NOT NULL  COMMENT '英文名称（例：Outdoor Run）',
    `gps_enabled` BOOL NOT NULL  COMMENT '是否使用GPS（True表示记录经纬度）' DEFAULT 1,
    `default_pace_unit` VARCHAR(10) NOT NULL  COMMENT '默认配速单位' DEFAULT 'min/km'
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `users` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `created_at` DATETIME(6) NOT NULL  DEFAULT CURRENT_TIMESTAMP(6),
    `updated_at` DATETIME(6) NOT NULL  DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `username` VARCHAR(64) NOT NULL UNIQUE,
    `password` VARCHAR(255),
    `email` VARCHAR(255)  UNIQUE,
    `phone` VARCHAR(20),
    `avatar` VARCHAR(500),
    `is_active` BOOL NOT NULL  DEFAULT 1
) CHARACTER SET utf8mb4 COMMENT='用户表 - 核心用户信息';
CREATE TABLE IF NOT EXISTS `oauth_accounts` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `created_at` DATETIME(6) NOT NULL  DEFAULT CURRENT_TIMESTAMP(6),
    `updated_at` DATETIME(6) NOT NULL  DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `provider` VARCHAR(32) NOT NULL,
    `provider_user_id` VARCHAR(255) NOT NULL,
    `access_token` VARCHAR(500),
    `refresh_token` VARCHAR(500),
    `expires_at` DATETIME(6),
    `extra_data` JSON,
    `user_id` INT NOT NULL,
    UNIQUE KEY `uid_oauth_accou_provide_41dfbe` (`provider`, `provider_user_id`),
    CONSTRAINT `fk_oauth_ac_users_b0d263ac` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
    KEY `idx_oauth_accou_user_id_41665f` (`user_id`),
    KEY `idx_oauth_accou_provide_41dfbe` (`provider`, `provider_user_id`)
) CHARACTER SET utf8mb4 COMMENT='第三方登录账号表';
CREATE TABLE IF NOT EXISTS `user_training_records` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID',
    `created_at` DATETIME(6) NOT NULL  DEFAULT CURRENT_TIMESTAMP(6),
    `updated_at` DATETIME(6) NOT NULL  DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `user_id` INT NOT NULL  COMMENT '用户ID',
    `start_time` DATETIME(6) NOT NULL  COMMENT '训练开始时间',
    `end_time` DATETIME(6) NOT NULL  COMMENT '训练结束时间',
    `duration` INT NOT NULL  COMMENT '训练时长（秒）',
    `total_distance` DECIMAL(6,2) NOT NULL  COMMENT '总距离（公里）',
    `average_pace` DECIMAL(4,2)   COMMENT '平均配速（分钟/公里）',
    `average_speed` DECIMAL(4,2)   COMMENT '平均速度（公里/小时）',
    `max_speed` DECIMAL(4,2)   COMMENT '最大速度（公里/小时）',
    `calories` DECIMAL(6,2)   COMMENT '消耗卡路里（千卡）',
    `sport_type_id` INT NOT NULL COMMENT '关联的运动类型ID',
    CONSTRAINT `fk_user_tra_sport_ty_34ffeb81` FOREIGN KEY (`sport_type_id`) REFERENCES `sport_types` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `aerich` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `version` VARCHAR(255) NOT NULL,
    `app` VARCHAR(100) NOT NULL,
    `content` JSON NOT NULL
) CHARACTER SET utf8mb4;"""


async def downgrade(db: BaseDBAsyncClient) -> str:
    return """
        """
